/*
 * Copyright (C) 2015-2018 Alibaba Group Holding Limited
 */

#include "aos/mbmaster.h"

#if (MBMASTER_CONFIG_RTU_ENABLED > 0)
int aos_mbmaster_rtu_init(mb_handler_t **handler, uint8_t port, uint32_t baud_rate, mb_parity_t parity)
{
    mb_status_t status;

    if (handler == NULL) {
        return -1;
    }

    status = mbmaster_rtu_init(handler, port, baud_rate, parity);
    if (status != MB_SUCCESS) {
        return -1;
    }

    return 0;
}

int aos_mbmaster_rtu_uninit(mb_handler_t *req_handler)
{
    mb_status_t status;

    if (req_handler == NULL) {
        return -1;
    }

    status = mbmaster_rtu_uninit(req_handler);
    if (status != MB_SUCCESS) {
        return -1;
    }

    return 0;
}
#endif /* MBMASTER_CONFIG_RTU_ENABLED */

#if (MBMASTER_CONFIG_READ_HOLDING > 0)
int aos_mbmaster_read_holding_reginster(mb_handler_t *req_handler, uint8_t slave_addr,
                                        uint16_t start_addr, uint16_t quantity, uint8_t *respond_buf,
                                        uint8_t *respond_count)
{
    mb_status_t status;

    if (req_handler == NULL) {
        return -1;
    }
	req_handler->resopnd_exp_len = 8 + quantity * 2 -3; 
    status = mbmaster_read_holding_reginster(req_handler, slave_addr, start_addr,
                                                 quantity, respond_buf, respond_count);
    if (status != MB_SUCCESS) {
        //printf("slave %d:%s\n", slave_addr,status_to_string(status));
        return -1;
    }

    return 0;
}

int aos_mbmaster_write_holding_reginster(mb_handler_t *req_handler, uint8_t slave_addr,
                                        uint16_t start_addr, uint16_t quantity, int16_t *buf, uint8_t *respond_buf,
                                        uint8_t *respond_count)
{
    mb_status_t status;

    if (req_handler == NULL) {
        return -1;
    }

	req_handler->resopnd_exp_len = 8;
    status = mbmaster_write_holding_reginster(req_handler, slave_addr, start_addr,
                                                 quantity, buf, respond_buf, respond_count);
    if (status != MB_SUCCESS) {
        //printf("slave %d:%s\n", slave_addr,status_to_string(status));
        return -1;
    }

    return 0;
}						

int aos_mbmaster_read_coil_reginster(mb_handler_t *req_handler, uint8_t slave_addr,
										uint16_t start_addr, uint16_t quantity, uint8_t *respond_buf,
										uint8_t *respond_count)
{
	mb_status_t status;

	if (req_handler == NULL) {
		return -1;
	}
	req_handler->resopnd_exp_len = 8 + quantity * 2 -3; 
	status = mbmaster_read_coil_reginster(req_handler, slave_addr, start_addr,
												 quantity, respond_buf, respond_count);
	if (status != MB_SUCCESS) {
		printf("slave %d:%s\n", slave_addr,status_to_string(status));
		return -1;
	}

	return 0;
}

		

int aos_mbmaster_write_coil_reginster(mb_handler_t *req_handler, uint8_t slave_addr,
										uint16_t start_addr, uint16_t val, uint8_t *respond_buf,
										uint8_t *respond_count)
{
	mb_status_t status;

	if (req_handler == NULL) {
		return -1;
	}

	req_handler->resopnd_exp_len = 8;
	status = mbmaster_write_coil_reginster(req_handler, slave_addr, start_addr,
												 val, respond_buf, respond_count);
	if (status != MB_SUCCESS) {
		printf("slave %d:%s\n", slave_addr,status_to_string(status));
		return -1;
	}

	return 0;
}


#endif /* MBMASTER_CONFIG_READ_HOLDING */

/*
aos api 
be in common use for modbus read reginster
param:
	req_handler:modbus handler
	fun_code:modbus function code
	slava_id:slave device id 
	start_addr:start address
	read_len:read length
	res_buf:recive read data buffer
	res_len:recove data length
return:
    0=success

*/
int aos_mbmaster_single_read_reginster(mb_handler_t *req_handler,uint8_t slave_id,uint16_t start_addr,
                                uint16_t read_len,uint8_t *res_buf,uint8_t *res_len){
    mb_status_t status;

    if (req_handler == NULL) {
        return -1;
    }
	req_handler->resopnd_exp_len = 8 + read_len * 2 -3; 
    status = mbmaster_single_read_reginster(req_handler,slave_id,start_addr,1,res_buf,res_len);
    if (status != MB_SUCCESS) {
        printf("slave %d:%s\n", slave_id,status_to_string(status));
        return -1;
    }

    return 0;
}


/*
aos api
be in common usefor modbus write reginster
	req_handler:modbus hander
	slava_id:slave device id
	fun_code:function code
	start_addr:register start address
	w_buf：write buffer
	w_len:write length
	res_buf:recive data
	res_len:recive data length pointer
return:
    0=success

*/
int aos_mbmaster_single_write_reginster(mb_handler_t *req_handler,uint8_t slave_id,uint16_t start_addr,
                                uint16_t *w_buf,uint16_t w_len,uint8_t *res_buf,uint8_t *res_len){
    mb_status_t status;

    if (req_handler == NULL) {
        return -1;
    }

	req_handler->resopnd_exp_len = 8;
    status = mbmaster_single_write_reginster(req_handler,slave_id,start_addr,w_buf,1,res_buf,res_len);
    if (status != MB_SUCCESS) {
        printf("slave %d:%s\n", slave_id,status_to_string(status));
        return -1;
    }

    return 0;
}



